package com.yoouce.patchmng.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.yoouce.patchmng.bean.UserInfo;
import com.yoouce.patchmng.init.SystemContants;
import com.yoouce.patchmng.util.DateTools;

/**
 * 补丁管理控制类 Created by administrator on 15-9-29.
 */
@Controller
public class PatchController_BackUp extends BaseController
{
    
    /**@RequestMapping(value = "/uploadPatchFile")
    @ResponseBody
    public Object uploadPatchFile(
            @RequestParam(value = "uploadFile") MultipartFile uploadFile,
            HttpServletRequest request)
    {
        
        // 获取当前用户信息
        UserInfo userInfo = (UserInfo) getRequest().getSession()
                .getAttribute(SystemContants.USER_KEY);
        Map<String, Object> resMap = new HashMap<String, Object>();
        
        if (uploadFile != null)
        {
            // 获取保存的路径，
            String realPath = request.getSession()
                    .getServletContext()
                    .getRealPath("/upload/patch/" + userInfo.getUserName() + "-"
                            + DateTools
                                    .getCurDay(DateTools.yyyy_MM_dd_HH_mm_ss));
            if (uploadFile.isEmpty())
            {
                // 未选择文件
                resMap.put("status", "Empty");
            }
            else
            {
                // 文件原名称
                String originFileName = uploadFile.getOriginalFilename();
                try
                {
                    // 这里使用Apache的FileUtils方法来进行保存
                    FileUtils.copyInputStreamToFile(uploadFile.getInputStream(),
                            new File(realPath, originFileName));
                    resMap.put("realPath", realPath);
                    resMap.put("status", "OK");
                }
                catch (IOException e)
                {
                    System.out.println("文件上传失败");
                    resMap.put("status", "NOK");
                    e.printStackTrace();
                }
            }
        }
        return resMap;
    }
    
    @RequestMapping(value = "/patchFiles")
    public String patchFiles()
    {
        // 获取当前用户信息
        UserInfo userInfo = (UserInfo) getRequest().getSession()
                .getAttribute(SystemContants.USER_KEY);
        // 判断是否已经存在地址和补丁名称
        Object patchName = getSession().getAttribute("patchName");
        String patchPath = "";
        if (patchName == null)
        {
            // 设置此次补丁文件名称
            patchName = userInfo.getUserName() + "-"
                    + DateTools.getCurDay(DateTools.yyyy_MM_dd_HH_mm_ss);
            patchPath = request.getSession().getServletContext().getRealPath(
                    "/upload/patch/" + userInfo.getUserName() + "-" + DateTools
                            .getCurDay(DateTools.yyyy_MM_dd_HH_mm_ss));
            getSession().setAttribute("patchName", patchName);
            getSession().setAttribute("patchPath", patchPath);
        }
        
        return "createPatch";
    }**/
}
